//
//  LGMedicineBallSMSController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/29.
//  发送短信提醒

#import "LGMedicineBallSMSController.h"
#import "DatePickerView.h"
@interface LGMedicineBallSMSController ()
/**   */
@property (nonatomic , strong)UIView *smsView;
/**   */
@property (nonatomic , strong)UILabel *smsLabel;
/**   */
@property (nonatomic , strong)UIButton *dateView;
/**   */
@property (nonatomic , strong)UITextField *dateTF;
/**   */
@property (nonatomic , strong)UIView *bottomView;
/**   */
@property (nonatomic , strong)UIButton *sendButton;
/**   */
@property (nonatomic , copy)NSString *content;

@end

@implementation LGMedicineBallSMSController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.dcb.patient/getSmsMessage" parameters:@{@"plan_ids":self.planids} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.content = LGNSString(returnData[@"message"]);
            self.smsLabel.text = self.content;
            [self.smsLabel lineSpacing:3];
            CGFloat height = [self.content textHeightWithFont:LGFont(16) maxWidth:Screen_W-viewPix(56) lineSpacing:3];
            self.smsView.frame = CGRectMake(viewPix(16), topBarHeight+viewPix(16), Screen_W-viewPix(32), viewPix(72)+height);
        }
    } error:^(NSError *error) {
        
    }];
}

//选择时间
-(void)selectDateAction{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd";
    DatePickerView *pickerView = [[DatePickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.beginDate = [formatter stringFromDate:[NSDate date]];
    [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
    __weak typeof(self) weakSelf = self;
    pickerView.selectDate = ^(NSString *date) {
        weakSelf.dateTF.text = date;
        weakSelf.sendButton.selected = NO;
        NSString *dateString = [date stringByReplacingCharactersInRange:NSMakeRange(4, 1) withString:@"年"];
        dateString = [dateString stringByReplacingOccurrencesOfString:@"-" withString:@"月"];
        dateString = [NSString stringWithFormat:@" %@日 ",dateString];
        NSString *content = [weakSelf.content stringByReplacingOccurrencesOfString:@"______" withString:dateString];
        weakSelf.smsLabel.text = content;
        [weakSelf.smsLabel lineSpacing:3];
        CGFloat height = [content textHeightWithFont:LGFont(16) maxWidth:Screen_W-viewPix(56) lineSpacing:3];
        weakSelf.smsView.frame = CGRectMake(viewPix(16), topBarHeight+viewPix(16), Screen_W-viewPix(32), viewPix(72)+height);
    };
}

//发送
-(void)sendButtonAction:(UIButton *)sender{
    if(sender.selected == YES){
        [TooltipView showMessage:@"请选择安排入院时间" offset:0];
    }else{
        [RequestUtil withPOST:@"/doctor/patient.dcb.patient/sendSms" parameters:@{@"plan_ids":self.planids,@"appointment_date":self.dateTF.text} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshMedicinBallData" object:nil];
                [TooltipView showMessage:@"发送成功" offset:0];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            [TooltipView showMessage:@"发送失败，请重试" offset:0];
        }];
    }
}


#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.navigationView.title = @"发送短信提醒";
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [UIColor colorWithString:@"#F2F3F5"];
    self.navigationView.backgroundColor = [UIColor colorWithString:@"#E5F3FF"];
    [self.view addSubview:self.navigationView];
    [self.view addSubview:self.smsView];
    [self.smsView addSubview:self.smsLabel];
    [self.smsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.smsView).offset(viewPix(60));
        make.left.equalTo(self.smsView).offset(viewPix(12));
        make.right.equalTo(self.smsView).offset(-viewPix(12));
    }];
    [self creatSmsTitleView];
    [self.view addSubview:self.dateView];
    [self.dateView addSubview:self.dateTF];
    UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip12"]];
    [self.dateView addSubview:tipView];
    [self.dateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.smsView.mas_bottom).offset(viewPix(16));
        make.left.right.equalTo(self.smsView);
        make.height.equalTo(@(viewPix(56)));
    }];
    [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dateTF.mas_right);
        make.centerY.equalTo(self.dateTF);
        make.width.height.equalTo(@(viewPix(12)));
    }];
    [self.view addSubview:self.bottomView];
    [self.bottomView addSubview:self.sendButton];
    
}
-(UIView *)smsView{
    if(!_smsView){
        _smsView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(16), topBarHeight+viewPix(16), Screen_W-viewPix(32), viewPix(192))];
        [_smsView setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _smsView.backgroundColor = [UIColor whiteColor];
    }
    return _smsView;
}

-(UILabel *)smsLabel{
    if(!_smsLabel){
        _smsLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _smsLabel;
}

-(void)creatSmsTitleView{
    UIButton *titleBtn = [UIButton buttonWithTitle:@"短信预览" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#333333" imageName:@"短信提醒-信息"];
    titleBtn.frame = CGRectMake(viewPix(12), viewPix(12), viewPix(150), viewPix(24));
    titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    titleBtn.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(6), 0, 0);
    titleBtn.userInteractionEnabled = NO;
    [self.smsView addSubview:titleBtn];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(12), viewPix(48), Screen_W-viewPix(56), 0.5)];
    lineView.backgroundColor = [UIColor colorWithString:@"#E8E8E8"];
    [self.smsView addSubview:lineView];
}

-(UIButton *)dateView{
    if(!_dateView){
        _dateView = [UIButton buttonWithTitle:@"安排入院时间" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#333333" imageName:@"短信提醒-时间"];
        _dateView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _dateView.imageEdgeInsets = UIEdgeInsetsMake(0, viewPix(12), 0, 0);
        _dateView.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(18), 0, 0);
        _dateView.backgroundColor = [UIColor whiteColor];
        _dateView.cornerRidus = 15;
        [_dateView addTarget:self action:@selector(selectDateAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _dateView;
}

-(UITextField *)dateTF{
    if(!_dateTF){
        _dateTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(150), 0, Screen_W-viewPix(202), viewPix(56))];
        _dateTF.textColor = [UIColor colorWithString:@"#333333"];
        _dateTF.textAlignment = NSTextAlignmentRight;
        _dateTF.userInteractionEnabled = NO;
        _dateTF.placeholder = @"请选择";
        _dateTF.font = LGFont(16);
    }
    return _dateTF;
}

-(UIView *)bottomView{
    if(!_bottomView){
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, Screen_H-viewPix(72)-bottomSafeBarHeight/4.0, Screen_W, viewPix(72))];
        _bottomView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomView;
}

-(UIButton *)sendButton{
    if(!_sendButton){
        _sendButton = [UIButton buttonWithTitle:@"发送" topMargin:viewPix(10)];
        _sendButton.selected = YES;
        [_sendButton addTarget:self action:@selector(sendButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendButton;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
